##########################################
###Autocratic Elections
###Stabilizing Tool or Force for Change
###Appendix tables
#########################################
rm(list = ls())
library(foreign);library(pspline);library(survival);library(MCMCpack)
library(ZeligGAM);library(R2jags)
library(mgcv);library(glmnet)
library(bootstrap);library(plyr);library(stargazer)

data <- read.dta("ReplicationData.dta")
dataallelections <- read.dta("ReplicationDataAllElections.dta")


logit.model.6 <- glm(gwf_fail ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 +  duration + duration2 + duration3
                     + gwf_personal + gwf_military + gwf_monarch,
                     data=data,family=binomial(link = "logit"))
summary(logit.model.6)
logit.model.7 <- glm(gwf_fail ~ election + election5year + regiondem + loggdp + gdp_grow + 
                      cow_milsize + resdep2 +  duration + duration2 + duration3 +
                       gwf_personal + gwf_military + gwf_monarch,
                     data=data, family=binomial(link = "logit"))
summary(logit.model.7)

logit.model.8 <- glm(gwf_fail ~ proxelection1 + proxelection8  + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 + sip2 +  duration + duration2 + duration3 + 
                        gwf_personal + gwf_military + gwf_monarch + as.factor(region) + as.factor(decade),
                     data=data,family=binomial(link = "logit"))
summary(logit.model.8)
logit.model.9 <- glm(gwf_fail ~ election + election5year + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 + sip2 + duration + duration2 + duration3 + 
                       gwf_personal + gwf_military + gwf_monarch + as.factor(region) + as.factor(decade) ,
                     data=data,family=binomial(link = "logit"))
summary(logit.model.9)


stargazer(logit.model.6,logit.model.7,logit.model.8,logit.model.9,
          out="../Results/LogitResultsRobustness1.tex",dep.var.caption="",dep.var.labels="Regime failure",
          no.space=TRUE,align=TRUE, float=FALSE,font.size="tiny",
          covariate.labels = c("Proximity to election / 1 ", "Proximity to election / 8",  "Election" , "Election 5 year", 
                               "Region Polity","ln(GDP per capita)", 
                               "GDP Growth", "Military size","Resource dependence", "SIP2",
                               "Duration","Duration$^2$","Duration$^3$",
                               "Personalist", "Military", "Monarchy") ,
          notes="Random Effects logit regressions with Geddes-Wright-Frantz (GWF; 2014) regime failure as dependent variable.")

logit.model.10 <- glm(gwf_fail ~ proxelection1 + proxelection8,
                      data=data,family=binomial(link = "logit"))
summary(logit.model.10)

logit.model.11 <- glm(gwf_fail ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                        cow_milsize + resdep2,
                      data=data,family=binomial(link = "logit"))
summary(logit.model.10)

logit.model.12 <- glm(gwf_fail ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                        resdep2 + duration + duration2 + duration3,
                      data=data,family=binomial(link = "logit"))
summary(logit.model.11)

logit.model.13 <- glm(gwf_fail ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                       cow_milsize +  duration + duration2 + duration3 ,
                     data=data,family=binomial(link = "logit"))
summary(logit.model.12)


logit.model.14 <- glm(gwf_fail ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 +  duration + duration2 + duration3 + urban,
                     data=data,family=binomial(link = "logit"))
summary(logit.model.13)

  

logit.model.15 <- glm(gwf_fail ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 +  duration + duration2 + duration3 + aidpergdp,
                     data=data,family=binomial(link = "logit"))
summary(logit.model.14)

logit.model.16 <- glm(gwf_fail ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                        cow_milsize + resdep2 +  duration + duration2 + duration3 + governmentconsumption,
                      data=data,family=binomial(link = "logit"))
summary(logit.model.15)

logit.model.17 <- glm(gwf_fail ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                        cow_milsize + resdep2 +  duration + duration2 + duration3 + tradeopenness,
                      data=data,family=binomial(link = "logit"))
summary(logit.model.16)


logit.model.18 <- glm(regtrans ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 +  duration + duration2 + duration3 ,
                     data=data,family=binomial(link = "logit"))
summary(logit.model.2)

stargazer(logit.model.10,logit.model.11,logit.model.12, logit.model.13,logit.model.14, logit.model.15,logit.model.16,logit.model.17,logit.model.18,
          out="../Results/LogitResultsRobustnes2.tex",dep.var.caption="",dep.var.labels="Regime failure",
          no.space=TRUE,align=TRUE, font.seize= "scriptsize",float=FALSE,font.size="tiny",
          covariate.labels = c("Proximity to election / 1 ", "Proximity to election / 8", 
                               "Region Polity","ln(GDP per capita)", 
                               "GDP Growth", "Military size","Resource dependence",
                               "Duration","Duration$^2$","Duration$^3$", "Urbanization",
                               "Aid / GDP", "Public spending", "Trade openness" ),
          
          notes="Logit regressions with Geddes-Wright-Frantz (GWF; 2014) regime failure as dependent variable.")


## poor mans jackknife
bootstrapdata <- na.omit(subset(data, select=c(gwf_fail, proxelection1, proxelection8, regiondem, loggdp, 
                        gdp_grow,cow_milsize, resdep2,  duration, duration2, duration3,gwno)))

table <- matrix(NA, nrow=115, ncol=3)
gwnotable <- unique(bootstrapdata$gwno)


bootstrapdata <- ddply(bootstrapdata,.(gwno),transform,gwno2=seq(gwno))
gwno <- bootstrapdata$gwno2

table[,1] <- gwnotable
for (i in 1:115){
  fit <- glm(gwf_fail ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
               cow_milsize + resdep2 +  duration + duration2 + duration3 ,
             data=bootstrapdata[gwno!=i,],family=binomial(link = "logit"))
  
  table[i,2] <- as.numeric(round((fit$coefficients[2]), digits=2))
  table[i,3] <- as.numeric(round((fit$coefficients[3]), digits=2))
  
}
table <- as.data.frame(table)
proxelection1 <- as.vector(table[,2])
proxelection8 <- as.vector(table[,3])
pdf("../Figures/Jackknifeproximity1.pdf")
hist(proxelection1)
dev.off()
pdf("../Figures/Jackknifeproximity8.pdf")
hist(proxelection8)
dev.off()


## ELECTION STOCK ETC

logit.model.1 <- glm(gwf_fail ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 +  duration + duration2 + duration3 + 
                       electioncount,
                     data=data,family=binomial(link = "logit"))
summary(logit.model.1)

logit.model.4 <- glm(gwf_fail ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 +  duration + duration2 + duration3 + 
                       firstelection,
                     data=data,family=binomial(link = "logit"))

logit.model.2 <- glm(gwf_fail ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 +  duration + duration2 + duration3 + sevenyeardummy,
                     data=data,family=binomial(link = "logit"))


onlyelectoralregimes <- subset(data, electionregime==1)
logit.model.3 <- glm(gwf_fail ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 +  duration + duration2 + duration3, 
                     data=onlyelectoralregimes,family=binomial(link = "logit"))


stargazer(logit.model.1,logit.model.4,logit.model.2,logit.model.3,
          out="../Results/LogitResultsRobustnes4.tex",dep.var.caption="",dep.var.labels="Regime failure",
          no.space=TRUE,align=TRUE, float=FALSE,font.size="tiny",
          covariate.labels = c("Proximity to election / 1 ", "Proximity to election / 8", 
                               "Region Polity","ln(GDP per capita)", 
                               "GDP Growth", "Military size","Resource dependence",
                               "Duration","Duration$^2$","Duration$^3$",
                               "Sum elections", "First election", "7 years"
                                ))


## Alternative DVs

logit.model.1 <- glm(irregularexit ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 +  duration + duration2 + duration3 ,
                     data=data,family=binomial(link = "logit"))
summary(logit.model.1)

logit.model.2 <- glm(regtrans ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 +  duration + duration2 + duration3 ,
                     data=data,family=binomial(link = "logit"))
summary(logit.model.2)

stargazer(logit.model.2,
          out="../Results/LogitResultsRobustnes5.tex",dep.var.caption="",
          no.space=TRUE,align=TRUE, float=FALSE,font.size="tiny",
          covariate.labels = c("Proximity to election / 1 ", "Proximity to election / 8", 
                               "Region Polity","ln(GDP per capita)", 
                               "GDP Growth", "Military size","Resource dependence",
                               "Duration","Duration$^2$","Duration$^3$" ))

## END Alternative DVs

## TESTING OTHER POTENTIAL MECHANISMS



logit.model.1 <- glm(gwf_fail ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 +  duration + duration2 + duration3 ,
                     data=data,family=binomial(link = "logit"), subset=data$electionregime==1)

logit.model.2 <- glm(gwf_fail ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 +  duration + duration2 + duration3 ,
                     data=data,family=binomial(link = "logit"), subset=data$electioncount>=2)

logit.model.3 <- glm(gwf_fail ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 +  duration + duration2 + duration3 ,
                     data=data,family=binomial(link = "logit"), subset=data$gwf_duration>=5)

logit.model.4 <- glm(gwf_fail ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 +  duration + duration2 + duration3 ,
                     data=data,family=binomial(link = "logit"), subset=data$timesinceelection<=data$gwf_duration)

logit.model.5 <- glm(gwf_fail ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 +  duration + duration2 + duration3 ,
                     data=data,family=binomial(link = "logit"), subset=data$ourtype!=3)

##Only democratizing failues


logit.model.6 <- glm(gwf_fail ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 +  duration + duration2 + duration3 + electioncount,
                     data=data,family=binomial(link = "logit"))

logit.model.7 <- glm(gwf_democratization  ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 +  duration + duration2 + duration3 ,
                     data=data,family=binomial(link = "logit"),subset=data$ourtype!=3)


logit.model.8 <- glm(gwf_democratization  ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 +  duration + duration2 + duration3 ,
                     data=data,family=binomial(link = "logit"))

logit.model.9 <- glm(gwf_democratization  ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2,
                     data=data,family=binomial(link = "logit"))

stargazer(logit.model.1,logit.model.2,logit.model.3,logit.model.4,logit.model.5,
          logit.model.6,logit.model.7,logit.model.8,logit.model.9,
          out="../Results/LogitResultsRobustnes7.tex", dep.var.caption="",
          dep.var.labels=c("Regime failure","Democratization"),
          no.space=TRUE,align=TRUE, float=FALSE,font.size="tiny",
          covariate.labels = c("Proximity to election / 1 ", "Proximity to election / 8", 
                               "Region Polity","ln(GDP per capita)", 
                               "GDP Growth", "Military size","Resource dependence",
                               "Duration","Duration$^2$","Duration$^3$", 
                               "Sum elections" ))

## END TESTING OTHER POTENTIAL MECHANISMS


